﻿
// EmbossedPictureView.cpp: CEmbossedPictureView 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "EmbossedPicture.h"
#endif

#include "EmbossedPictureDoc.h"
#include "EmbossedPictureView.h"
#include "Resource.h"
#pragma comment(lib,"vfw32.lib")
#include"ImageProcessDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CEmbossedPictureView

IMPLEMENT_DYNCREATE(CEmbossedPictureView, CView)

BEGIN_MESSAGE_MAP(CEmbossedPictureView, CView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
	ON_COMMAND(ID_IMAGEDRAW, &CEmbossedPictureView::OnImagedraw)
END_MESSAGE_MAP()

// CEmbossedPictureView 构造/析构

CEmbossedPictureView::CEmbossedPictureView() noexcept
{
	// TODO: 在此处添加构造代码

}

CEmbossedPictureView::~CEmbossedPictureView()
{
}

BOOL CEmbossedPictureView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CView::PreCreateWindow(cs);
}

// CEmbossedPictureView 绘图

void CEmbossedPictureView::OnDraw(CDC* pDC)
{
	CEmbossedPictureDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: 在此处为本机数据添加绘制代码

	COLORREF* pColor;
	HBITMAP hSrcbmp;
	BITMAP bm;
	CBitmap bmp;

	m_hDrawDib = DrawDibOpen();
	bmp.LoadBitmap(IDB_BMP_MYLOVE);
	bmp.GetBitmap(&bm);
	BITMAPINFOHEADER RGB32BITSBITMAPINFO = { sizeof(BITMAPINFOHEADER),
		bm.bmWidth,bm.bmHeight,1,32,BI_RGB,0,0,0,0 };

	HDC hMemDc = CreateCompatibleDC(NULL);
	hSrcbmp = CreateDIBSection(hMemDc,(BITMAPINFO*)&RGB32BITSBITMAPINFO,DIB_RGB_COLORS,(VOID**)&pColor,NULL,0);
	if (hSrcbmp)
	{
		HBITMAP hOldBmp =(HBITMAP)SelectObject(hMemDc,hSrcbmp);
		HDC hDC = CreateCompatibleDC(hMemDc);
		if (hDC)
		{
			HBITMAP hOldBmp2 = (HBITMAP)SelectObject(hDC,bmp);
			StretchBlt(hMemDc, 0, 0, bm.bmWidth, bm.bmHeight, hDC, 0, 0,bm.bmWidth,bm.bmHeight,SRCCOPY);
			SelectObject(hDC,hOldBmp2);
			DeleteDC(hDC);
		}
	}

	if (m_hDrawDib)
	{
		int i, j, k, cx = bm.bmWidth;
		int r[2], g[2], b[2];
		int r0, g0, b0;
		for (i = 0; i < bm.bmWidth; i++)
		{
			for (j = 0; j < bm.bmHeight-1; j++)
			{
				r[0] = GetRValue(pColor[i + j * cx]);
				g[0] = GetGValue(pColor[i + j * cx]);
				b[0] = GetBValue(pColor[i + j * cx]);
				r[1] = GetRValue(pColor[(i + 1) + (j + 1) * cx]);
				g[1] = GetGValue(pColor[(i + 1) + (j + 1) * cx]);
				b[1] = GetBValue(pColor[(i + 1) + (j + 1) * cx]);
				r0 = r[1] - r[0] + 128;
				g0 = g[1] - g[0] + 128;
				b0 = b[1] - b[0] + 128;
				if (r0 > 255)
					r0 = 255;
				else if (r0 < 0)
					r0 = 0;
				if (g0 > 255)
					g0 = 255;
				else if (g0 < 0)
					g0 = 0;
				if (b0 > 255)
					b0 = 255;
				else if (b0 < 0)
					b0 = 0;
				pColor[i + j * cx] = RGB(r0, g0, b0);
			}
		}	
		DrawDibDraw(m_hDrawDib, pDC->GetSafeHdc(), 0, 0, bm.bmWidth, bm.bmHeight, &RGB32BITSBITMAPINFO, (LPVOID)(pColor), 0, 0, bm.bmWidth, bm.bmHeight, DDF_BACKGROUNDPAL);
	}
	else
	{
		DrawDibDraw(m_hDrawDib, pDC->GetSafeHdc(), 0, 0, bm.bmWidth, bm.bmHeight, &RGB32BITSBITMAPINFO, (LPVOID)(pColor), 0, 0, bm.bmWidth, bm.bmHeight, DDF_BACKGROUNDPAL);
	}
	DrawDibClose(m_hDrawDib);
}


// CEmbossedPictureView 打印

BOOL CEmbossedPictureView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CEmbossedPictureView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CEmbossedPictureView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清理过程
}


// CEmbossedPictureView 诊断

#ifdef _DEBUG
void CEmbossedPictureView::AssertValid() const
{
	CView::AssertValid();
}

void CEmbossedPictureView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CEmbossedPictureDoc* CEmbossedPictureView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEmbossedPictureDoc)));
	return (CEmbossedPictureDoc*)m_pDocument;
}
#endif //_DEBUG


// CEmbossedPictureView 消息处理程序


void CEmbossedPictureView::OnImagedraw()
{
	// TODO: 在此添加命令处理程序代码
	CImageProcessDlg dlg;
	dlg.DoModal();

}
